# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Naveen M K <naveen521kk@gmail.com>

_realname=inkscape
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.2
pkgrel=2
_pkg_suffix=2022-12-01_b0a8486541
pkgdesc="Vector graphics editor using the SVG file format (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://inkscape.sourceforge.io/"
license=("GPL" "LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "intltool")
depends=("${MINGW_PACKAGE_PREFIX}-aspell"
         "${MINGW_PACKAGE_PREFIX}-double-conversion"
         "${MINGW_PACKAGE_PREFIX}-gc"
         "${MINGW_PACKAGE_PREFIX}-gdl"
         "${MINGW_PACKAGE_PREFIX}-ghostscript"
         "${MINGW_PACKAGE_PREFIX}-graphicsmagick"
         "${MINGW_PACKAGE_PREFIX}-gsl"
         "${MINGW_PACKAGE_PREFIX}-gtkmm3"
         "${MINGW_PACKAGE_PREFIX}-gtkspell3"
         "${MINGW_PACKAGE_PREFIX}-hicolor-icon-theme"
         "${MINGW_PACKAGE_PREFIX}-gtk-update-icon-cache"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-lib2geom"
         "${MINGW_PACKAGE_PREFIX}-libcdr"
         "${MINGW_PACKAGE_PREFIX}-librevenge"
         "${MINGW_PACKAGE_PREFIX}-libsoup"
         "${MINGW_PACKAGE_PREFIX}-libvisio"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-libwpg"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-popt"
         "${MINGW_PACKAGE_PREFIX}-potrace"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-scour"
         "${MINGW_PACKAGE_PREFIX}-omp")
optdepends=(#"${MINGW_PACKAGE_PREFIX}-pstoedit: latex formulas"
            "${MINGW_PACKAGE_PREFIX}-texlive-scheme-medium: latex formulas"
            "${MINGW_PACKAGE_PREFIX}-python-numpy: some extensions"
            "${MINGW_PACKAGE_PREFIX}-python-lxml: some extensions and filters"
            #"${MINGW_PACKAGE_PREFIX}-scour: optimized SVG output"
            #"${MINGW_PACKAGE_PREFIX}-uniconvertor: reading/writing to some proprietary formats"
            )
options=('staticlibs' 'strip')
source=("https://media.inkscape.org/dl/resources/file/${_realname}-${pkgver}.tar.xz"
        inkscape-1.0.1-unbundle.patch
        inkscape-1.0.1-install-layout.patch
        clang-fix-4.patch)
sha256sums=('a0c7fd0d03c0a21535e648ef301dcf80dd7cfc1f3545e51065fbf1ba3ee8a5c4'
            '67cffe472b93b011f5c5d46b30dfe9ebf7e3426dbe894390802eb225f4965616'
            '131b2e1190637df0554ef1ee8cf46440689584375c117d057ab47d5871c58128'
            '847dee197e295ddc44c8b5c17264666e65b8d311dcf01a9a8abe6a9887f89cca')
noextract=("${_realname}-${pkgver}.tar.xz")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  # Symlinks only in packaging/macos and src/3rdparty/cairo folders
  tar -xf ${_realname}-${pkgver}.tar.xz || true

  mv "${srcdir}"/${_realname}-${pkgver}_${_pkg_suffix} "${srcdir}"/${_realname}-${pkgver}
  cd "${srcdir}"/${_realname}-${pkgver}

  apply_patch_with_msg \
    inkscape-1.0.1-unbundle.patch \
    inkscape-1.0.1-install-layout.patch

  apply_patch_with_msg \
    clang-fix-4.patch
}

build() {
  [[ -d build-${MSYSTEM} ]] && rm -rf build-${MSYSTEM}
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  MSYS=winsymlinks:native \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DBUILD_TESTING=OFF \
    -DCMAKE_AR=${MINGW_PREFIX}/bin/ar \
    -DWITH_IMAGE_MAGICK=OFF \
    -DWITH_INTERNAL_CAIRO=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/COPYING "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
